package org.jeecg.modules.employment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.employment.entity.GwStudent;
import org.jeecg.modules.employment.entity.SysSettingEntity;
import org.jeecg.modules.employment.service.IGwStudentService;
import org.jeecg.modules.employment.service.ISysSettingService;
import org.jeecg.modules.firm.entity.GwFirm;
import org.jeecg.modules.firm.service.IGwFirmService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Calendar;
import java.util.List;

/**
* @Description: 就业
*/
@Api(tags="门户->就业信息")
@RestController
@RequestMapping("/api/home/employment")
@Slf4j
public class EmploymentController {

   @Resource
   private ISysSettingService iSysSettingService;

   @Resource
   private IGwStudentService iGwStudentService;

    @Resource
    IGwFirmService iGwFirmService;


   /**
    * 就业参数
    * @param req 请求参数
    * @return 系统设置参数信息
    */
   @ApiOperation(value="01. 就业信息数据获取（职业大典， 大学生就业指导、职业测评）", notes="就业信息数据获取")
   @GetMapping(value = "/01/setting")
   public Result<EmploymentResult> a1QueryPageList(HttpServletRequest req) {


       final List<SysSettingEntity> list = iSysSettingService.list();
       EmploymentResult result = new EmploymentResult();
       list.forEach(item -> {
           if(item.getSettingKey().equals("职业大典PDF")) {
               result.setProfessionalEncyclopediaPdf(item.getSettingValue());
           }
           if(item.getSettingKey().equals("大学生就业指导PDF")) {
               result.setEmploymentGuidancePdf(item.getSettingValue());
           }
           if(item.getSettingKey().equals("职业测评URL")) {
               result.setCareerQuiz(item.getSettingValue());
           }
       });
       return Result.OK(result);
   }

   @Data
   public static class EmploymentResult{

       @ApiModelProperty(value = "职业大典PDF")
       private String professionalEncyclopediaPdf = "";

       @ApiModelProperty(value = "大学生就业指导PDF")
       private String employmentGuidancePdf = "";

       @ApiModelProperty(value = "职业测评URL")
       private String careerQuiz = "https://www.baidu.com";
   }


    /**
     * 个人简历获取接口
     * @param req 请求参数
     * @return 个人简历获取接口
     */
    @ApiOperation(value="02. 个人简历获取接口", notes="个人简历获取接口")
    @GetMapping(value = "/02/person/resume")
    public Result<PersonResumeResult> a2QueryPersonResume(HttpServletRequest req) {

        SysSettingEntity parameter = new SysSettingEntity();
        parameter.setSettingKey("个人简历模版文件");
        QueryWrapper<SysSettingEntity> queryWrapper = QueryGenerator.initQueryWrapper(parameter, null);
        SysSettingEntity sysSettingEntity = iSysSettingService.getOne(queryWrapper);
        PersonResumeResult result = new PersonResumeResult();
        if(sysSettingEntity != null) {
            result.setResumeTemplate(sysSettingEntity.getSettingValue());
        }

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 查询个人简历
        final GwStudent gwStudent = iGwStudentService.getById(sysUser.getId());
        if(gwStudent != null) {
            result.setPersonResume(gwStudent.getPersonResumeUrl());
            result.setHaveUploadResume(Boolean.TRUE);
        } else {
            result.setHaveUploadResume(Boolean.FALSE);
        }

        return Result.OK(result);
    }


    /**
     * 上传简历参数
     * @param personResume 上传简历参数
     * @return 执行结果
     */
    @ApiOperation(value="03. 上传简历参数", notes="上传简历参数")
    @PostMapping(value = "/03/person/resume")
    public Result<Void> a3UploadPersonResume(@Valid @RequestBody PersonResumeResult personResume) {


        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 查询个人简历
        GwStudent gwStudent = iGwStudentService.getById(sysUser.getId());
        if(gwStudent == null) {
            gwStudent = new GwStudent();
            gwStudent.setId(sysUser.getId());
            gwStudent.setCreateBy(sysUser.getId());
            gwStudent.setCreateTime(Calendar.getInstance().getTime());
        }
        gwStudent.setUpdateTime(Calendar.getInstance().getTime());
        gwStudent.setUpdateBy(sysUser.getId());
        gwStudent.setPersonResumeUrl(personResume.getPersonResume());
        iGwStudentService.saveOrUpdate(gwStudent);
        return Result.OK(null);
    }


    /**
     * 个人简历
     */
    @Data
    public static class PersonResumeResult{

        @ApiModelProperty(value = "简历模版")
        private String resumeTemplate = "";

        @ApiModelProperty(value = "个人简历地址", required = true)
        @NotBlank(message = "[个人简历地址]不能为空！")
        private String personResume = "";

        @ApiModelProperty(value = "是否上传个人简历")
        private boolean haveUploadResume;
    }

    @ApiOperation(value="05. 在招聘企业-分页列表查询")
    @GetMapping(value = "/05/list")
    public Result<IPage<GwFirm>> a5QueryPageList05(GwFirm gwFirm,
                                               @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                               @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                               HttpServletRequest req) {
        QueryWrapper<GwFirm> queryWrapper = QueryGenerator.initQueryWrapper(gwFirm, req.getParameterMap());
        Page<GwFirm> page = new Page<GwFirm>(pageNo, pageSize);
        IPage<GwFirm> pageList = iGwFirmService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

}
